package rcs34.android.photopublishare;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import rcs34.android.libs.ExifDriver.ExifDriver;
import rcs34.android.libs.ExifDriver.ExifManager;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import Utils.ImageUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityExifEditor extends Activity {
	private final String LOGTAG = getClass().getName();
	private final int REQUEST_LOCATION = 23;
	private ProgressDialog progressDialog;
	private Location location;
	private EditText artist;
	private EditText copyrightAuthor;
	private EditText copyrightEditor;
	private EditText description;
	private EditText comment;
	private TextView gps;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exif_editor);
		artist=(EditText)findViewById(R.id.exifEditorArtist);
		copyrightAuthor=(EditText)findViewById(R.id.exifEditorCopyrightPhotographer);
		copyrightEditor=(EditText)findViewById(R.id.exifEditorCopyrightEditor);
		description=(EditText)findViewById(R.id.exifEditorDescription);
		comment=(EditText)findViewById(R.id.exifEditorComment);
		gps=(TextView)findViewById(R.id.exifEditorLocation);
		if(GApp.exifDriver!=null){
		  ExifManager exif = new ExifManager(GApp.exifDriver, GApp.context);
		  String value="";
		  value=exif.getArtist();
		  if(value!=null && !value.equals("")){ 
		  		artist.setText(exif.getArtist());
		  }
		  copyrightAuthor.setText(exif.getPhotographerCopyright());
		  copyrightEditor.setText(exif.getEditorCopyright());
		  description.setText(exif.getImageDescription());
		}
	}

	public void handleSet(View _view) {
		if (GApp.exifDriver != null) {
			progressDialog = ProgressDialog.show(this, "Saving profile", "");
			ProfileSetter setter = new ProfileSetter();
			setter.execute(GApp.exifDriver); 
		}
	}

	public void handleLocation(View _view) {
		Intent intent = new Intent(this, ActivityLocation.class);
		startActivityForResult(intent, REQUEST_LOCATION);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent _intent) {
		if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOCATION) {
			location = (Location) _intent.getExtras().get(GApp.EXTRA_GEOPOINT);
			Log.v(LOGTAG, "Ready to set " + location);
		}
	}

	private class ProfileSetter extends AsyncTask<ExifDriver, String, Integer> {
		@Override
		protected Integer doInBackground(ExifDriver... _drive) {
			ExifDriver driver = GApp.exifDriver;
			GApp.exifDriver = null;// not valid anymore
			Log.v(LOGTAG, "Setting profile");
			ExifManager exif = new ExifManager(driver, GApp.context);
			exif.setArtist(artist.getText().toString());
			exif.setCopyright(copyrightAuthor.getText().toString(), copyrightEditor.getText().toString());
			exif.setImageDescription(description.getText().toString());
			exif.setUserComment(comment.getText().toString());
			if(location!=null){
				exif.setGPSLocation(location.getLatitude(), location.getLongitude(), location.getAltitude());
			}
			File cacheDir = GApp.context.getCacheDir();
			File tempFile = null;
			try {
				tempFile = File.createTempFile("publishare_exif_save", null, cacheDir);
				driver.save(tempFile.getAbsolutePath());
				if (tempFile.exists()) {
					ImageUtils.copyFile(tempFile, new File(driver.getSourceFile()));
					tempFile.delete();
				}
			} catch (IOException e) {
				if (tempFile != null && tempFile.exists()) {
					tempFile.delete();
				}
				;
				e.printStackTrace();
			} finally {
				if (tempFile != null && tempFile.exists()) {
					tempFile.delete();
				}
				;
			}
			return 0;
		}

		@Override
		protected void onPostExecute(Integer _dummy) {
			progressDialog.dismiss();
			Intent result = new Intent();
			setResult(Activity.RESULT_OK, result);
			finish();
		}
	}

}
